/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushserver.filesend;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import simplepushserver.gui.ProgressListener;
import simplepushserver.gui.UpdateEvent;
/**
 *
 * @author Filip
 */
public class FileDownloader implements Runnable{
    private ProgressListener listener;
    private String targetURL;
    private File targetFolder;
    private File target;
    private int read = 0;
    private int avail = -1;

    public FileDownloader(String targetURL, File targetFolder) {
        this.targetURL = targetURL;
        this.targetFolder = targetFolder;
        this.listener = null;
    }

    public FileDownloader(String targetURL, File targetFolder, ProgressListener update) {
        this.targetURL = targetURL;
        this.targetFolder = targetFolder;
        this.listener = update;
    }


    public void run() {
        try {
            String name = getTargetURL().substring(getTargetURL().lastIndexOf('/'));
            target = new File(getTargetFolder(),name);
            URL u = new URL(getTargetURL());
            URLConnection uc = u.openConnection();
            avail = uc.getContentLength();
            java.io.BufferedInputStream in = new java.io.BufferedInputStream(uc.getInputStream());
            if(avail==0)
                avail=in.available();
            java.io.FileOutputStream fos = new java.io.FileOutputStream(target);
            java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(fos, 4096);
            int count;
            byte[] arr = new byte[2048];
            if(listener==null){
                while ((count = in.read(arr)) != -1){
                    bout.write(arr);
                    read += arr.length;
                }
            }else{
                UpdateEvent ue = new UpdateEvent(0, avail);
                int updates = 0;
                while ((count = in.read(arr)) != -1){
                    bout.write(arr);
                    read+=arr.length;
                    updates++;
                    if(updates==100){
                        ue.setNewValue(read);
                        listener.onUpdate(ue);
                        updates=0;
                    }
                }
            }
            bout.flush();
            fos.flush();
            fos.close();
            bout.close();
            in.close();
        }catch (IOException ex){
            if(listener==null){
                ex.printStackTrace();
            }else{
                listener.onUpdate(new UpdateEvent(read,avail,ex.getMessage()));
            }
        }
    }

    /**
     * @return the targetURL
     */
    public String getTargetURL() {
        return targetURL;
    }
    /**
     * @return the targetFolder
     */
    public File getTargetFolder() {
        return targetFolder;
    }

    public int getAvail() {
        return avail;
    }

    public int getRead() {
        return read;
    }

    public File getTarget() {
        return target;
    }
    
    public static void main(String[] args) {}
}
